<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【Debug】bitField 引发的栈溢出排错记 | 后端学习记录</title><meta name="keywords" content="Java"><meta name="author" content="h0ss"><meta name="copyright" content="h0ss"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1、背景前期因为布隆过滤器的实现需求，导入了 redisson 的依赖，后面项目需求迭代，需要用到 redis 的 bitmap 来做签到信息的存储，并且需要提供读取每月签到记录的功能，这里需要用 bitField 方法将位信息读取成 Long 数值，之后进行移位操作得到当月每天的签到情况，问题代码如下：  对于 bit 的 set 操作是没问题的，但是用到这个 bitField 就出问题了。 项">
<meta property="og:type" content="article">
<meta property="og:title" content="【Debug】bitField 引发的栈溢出排错记">
<meta property="og:url" content="https://blog.gpnusz.cn/2022/08/21/%E3%80%90Debug%E3%80%91bitField-%E5%BC%95%E5%8F%91%E7%9A%84%E6%A0%88%E6%BA%A2%E5%87%BA%E6%8E%92%E9%94%99%E8%AE%B0/index.html">
<meta property="og:site_name" content="后端学习记录">
<meta property="og:description" content="1、背景前期因为布隆过滤器的实现需求，导入了 redisson 的依赖，后面项目需求迭代，需要用到 redis 的 bitmap 来做签到信息的存储，并且需要提供读取每月签到记录的功能，这里需要用 bitField 方法将位信息读取成 Long 数值，之后进行移位操作得到当月每天的签到情况，问题代码如下：  对于 bit 的 set 操作是没问题的，但是用到这个 bitField 就出问题了。 项">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog.gpnusz.cn/img/avatar.jpg">
<meta property="article:published_time" content="2022-08-20T18:10:40.000Z">
<meta property="article:modified_time" content="2022-08-20T18:13:16.209Z">
<meta property="article:author" content="h0ss">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog.gpnusz.cn/img/avatar.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://blog.gpnusz.cn/2022/08/21/%E3%80%90Debug%E3%80%91bitField-%E5%BC%95%E5%8F%91%E7%9A%84%E6%A0%88%E6%BA%A2%E5%87%BA%E6%8E%92%E9%94%99%E8%AE%B0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-08-21 02:13:16'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">51</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">后端学习记录</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">【Debug】bitField 引发的栈溢出排错记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-08-20T18:10:40.000Z" title="发表于 2022-08-21 02:10:40">2022-08-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-20T18:13:16.209Z" title="更新于 2022-08-21 02:13:16">2022-08-21</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>7分钟</span></span></div></div></div><article class="post-content" id="article-container"><h2 id="1、背景"><a href="#1、背景" class="headerlink" title="1、背景"></a>1、背景</h2><p>前期因为布隆过滤器的实现需求，导入了 redisson 的依赖，后面项目需求迭代，需要用到 redis 的 bitmap 来做签到信息的存储，并且需要提供读取每月签到记录的功能，这里需要用 bitField 方法将位信息读取成 Long 数值，之后进行移位操作得到当月每天的签到情况，问题代码如下：</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/effa8f9aea5843ffbb9d2818c0c32b6b~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>对于 bit 的 set 操作是没问题的，但是用到这个 bitField 就出问题了。</p>
<p>项目的依赖版本信息如下：</p>
<table>
<thead>
<tr>
<th>依赖</th>
<th>版本</th>
</tr>
</thead>
<tbody><tr>
<td>springboot</td>
<td>2.4.12</td>
</tr>
<tr>
<td>spring-data-redis</td>
<td>2.4.14</td>
</tr>
<tr>
<td>lettuce-core</td>
<td>6.0.8.RELEASE</td>
</tr>
<tr>
<td>redisson-spring-boot-starter</td>
<td>3.11.5</td>
</tr>
</tbody></table>
<h2 id="2、现象"><a href="#2、现象" class="headerlink" title="2、现象"></a>2、现象</h2><p>做单元测试的时候发现在 bitField 方法会栈溢出，看堆栈信息是递归调用自己了：</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ebd0ca7b16aa41aeaab9fac5f2dbb3b3~tplv-k3u1fbpfcp-zoom-1.image"></p>
<h2 id="3、定位"><a href="#3、定位" class="headerlink" title="3、定位"></a>3、定位</h2><p>在图1的136行上打个断点debug看下，跟着 <code>stringRedisTemplate.opsForValue()</code> 方法可以来到 DefaultValueOperations 的 bitField 方法，398行是将key转换为字节数组形式可以直接跳过，我们直接在399行进去看看。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ee594fb88dca49f186e802a6af427dca~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>进到 AbstractOperations 的 execute 方法，接着进到 RedisTemplate 的 execute 方法，然后进入重载 execute 方法，重点看下这个方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/58d54e28a786480e9b785a73155e9601~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>关键是这里的工厂设置，可以看到这里是 redisson 的工厂。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ea9df40d08ff4c7a87e6c39944b46acf~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>前面是一些判断以及工厂的设置，209、214行是获取连接的操作，这里可以看到 conn 获取的是 RedissonConnection 对象，而connToUse 获取的是 DefaultStringRedisConnection 连接对象。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b0198f8f95b8478399db5fde1831d122~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>继续单步下去可以看到这里调用了 doInRedis 方法，并且传进去的是 DefaultStringRedisConnection 。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5e3f870cd6d34e8fa04d189cff4f9a8f~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>跟进去可以看到 doInRedis 实际上是个回调方法，回调到一开始的 <code>connection.bitField</code> 方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a6d889e7629f4dc28be89f116921cc3e~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>继续跟进去来到了 DefaultStringRedisConnection 的 bitField 方法，这里我们可以看到 delegate 是一个 RedissonConnection 对象，我们再跟进看看。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7d192f87a2684e8facc0f3264fca28db~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>来到的是接口的默认方法 bitField，再看看这里的 stringCommands 方法返回的是什么对象，</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a7c84b46c3bc4911ab46cb16dbe347e3~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>stringCommands方法实际上返回的还是 RedissonConnection，意味着<code>stringCommands().bitField(xx) </code>继续执行下去一直是在调用自己，别忘了我们刚刚是怎么进来的，就是在 delegate 为 RedissionConnection 的时候进到了这个接口的默认方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/14dbebd3349e4a9396f9e4a0b02a27ff~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>那么到这里我们可以发现，实际上是在接口的默认方法这里发生了递归调用，并且没有停止条件，导致最后的栈溢出，这一点也跟我们的堆栈信息是一致的。</p>
<h2 id="4、验证"><a href="#4、验证" class="headerlink" title="4、验证"></a>4、验证</h2><p>那么明明 delegate 是 RedissonConnection 对象，为什么会进到接口的默认方法呢？只有一种可能就是其中没有对接口进行实现。</p>
<p>为了验证我们看看 redissonConnection 中有没有实现这个方法：</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/418e3327b8ae4003a840292fc32e913a~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>搜一下可以发现只有三个bit开头的方法，并没有 bitField，这就导致只能循环调用到接口的默认方法。</p>
<p>这里对比之下我们在调用 set 方法时是没问题的，我们同样探究以下它的执行过程。首先是业务代码，就是一行简单的调用：</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/70ecd511c4f84770b272c293e78a0045~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>接着我们跟进去看 set 方法，可以看到跟 bitField 类似，进到 execute 看看。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/eb8ce10cc9ce45119ff2384a80446061~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>接着经过一系列的execute调用、重载到真正执行的方法 RedisTemplate.execute(xx)。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5d93970428364b4185d4bc76413333d9~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>可以看到这里的 connToExpose 也是 DefaultStringRedisConnection，接着跟进 doInRedis 方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/83cf6a1d30de471696f590cfe1c3b57d~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1112b9cff4f44af7a654d7df2e5eca95~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2f91410345f448c98162b16e6cbe9047~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>依旧是执行回调方法，进去 connection.set 方法看看。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2a8e0e024d814ac78cd74b6ab9ff40e9~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>来到了 DefaultStringRedisConnection 的 set 方法，可以看到这里的 delegate 同样是 RedissonConnection 。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e39c70de0d194cd8850de504788b2c95~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>跟进去可以看到，这里跟 bitField 不同了，不是进到接口的默认方法实现，而是到了 RedissonConnection 的 set 实现，也就代表着这里可以调用里面的执行流程了，不会无休止地调用接口的默认方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/486f835d6eff4b4586715b9556ca78ae~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>其实到这里已经可以结束了，但为了过程的完整我们继续跟下去。跟到 write 方法中可以看到这里是用到了异步的写入，我们跟进这个方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/f4aa902ebd3a4cd58f53820e239742a6~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>可以看到这里只是注册了异步的请求信息，并没有等待获取结果。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d4c769d769d34968b60f27c56a55ac6d~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>回到 write 方法继续往下走，实际上获取结果是在 sync 方法这里，进行同步等待。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3771eaa9569c43a880607c8f74eca45c~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>跟进可以看到这里返回的是 syncFuture 的结果，看名字可以猜到这个方法是同步的。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/d07939ed5ffc4acb9e7233f162613300~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>进到方法里面可以看到是调用刚刚注册好异步事件的服务的 get 方法进行结果获取。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/26ad47f67d144873814302ed743ececf~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>可以看到这里会await，直到有结果会被唤醒进入下面的获取操作，getNow里面就是获取返回值了。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4d5ed00ab9434381a128733dcfb27665~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>到这里 set 的操作就结束了，对比可以发现，正是因为 redisson 中没有实现 bitField 方法才导致的递归调用默认方法，进而导致栈溢出。</p>
<h2 id="5、解决"><a href="#5、解决" class="headerlink" title="5、解决"></a>5、解决</h2><h3 id="1）第一种解决方案"><a href="#1）第一种解决方案" class="headerlink" title="1）第一种解决方案"></a>1）第一种解决方案</h3><p>翻看 redisson 的 issue 记录可以发现在2021年提出 RedissonConnection 中缺少部分方法这个问题，那么</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/03c192b950504a158e6e965032b72215~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>可以看到在这一次的 commit 中对缺失的方法进行实现，其中就包括 bitField 方法。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6a1985a4ff3d49bf81a10efe59a8fb9d~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/94f7d3f5cd534ab6b867e2ebb0c59001~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>对照版本看一下，在 3.15.6 已经应用上了，也就是说如果使用的是 3.15.6 之后的版本就不存在这个问题了。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/86548aa8d9444fb49830c96eb06fe3f8~tplv-k3u1fbpfcp-zoom-1.image"></p>
<h3 id="2）第二种解决方案"><a href="#2）第二种解决方案" class="headerlink" title="2）第二种解决方案"></a>2）第二种解决方案</h3><p>实际上在未引入 redisson 之前这个方法是可用的，因为在 yml 文件中已经做了 lettuce 连接池的相关配置，那么会自动配置到 lettuce 工厂中。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8d363eb3606b4b67808d14cd97d63c27~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>从debug结果也可以看到，获取到的是 lettuceConnectionFactory，那么后续的 bitField 自然就没问题了，问题是引入了 redisson 之后工厂的实现就被替换为 redisson 了。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ba03776d67a64fe59ebd40a9ba01d5f7~tplv-k3u1fbpfcp-zoom-1.image"></p>
<p>如果要在当前版本解决这个问题，就需要对那些issue中提到的缺失的方法进行引导，让他们找到对应的实现。</p>
<p>简而言之，如果连接工厂是 lettuce 或者 jedis 就可以正常使用，因为其中有对 bitField 的实现，那么我们只要手动配置一个 lettuce 连接工厂即可，并且这不会应该到使用 redisson 的初衷——使用其中的布隆过滤器实现。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">package cn.gpnusz.userservice.config;</span><br><span class="line"></span><br><span class="line">import org.apache.commons.pool2.impl.GenericObjectPoolConfig;</span><br><span class="line">import org.springframework.beans.factory.annotation.Value;</span><br><span class="line">import org.springframework.context.annotation.Bean;</span><br><span class="line">import org.springframework.context.annotation.Configuration;</span><br><span class="line">import org.springframework.data.redis.connection.RedisPassword;</span><br><span class="line">import org.springframework.data.redis.connection.RedisStandaloneConfiguration;</span><br><span class="line">import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;</span><br><span class="line">import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;</span><br><span class="line">import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;</span><br><span class="line"></span><br><span class="line">@Configuration</span><br><span class="line">public class LettuceConfig &#123;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.host&#125;&quot;)</span><br><span class="line">    private String host;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.port&#125;&quot;)</span><br><span class="line">    private Integer port;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.password&#125;&quot;)</span><br><span class="line">    private String password;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.database&#125;&quot;)</span><br><span class="line">    private Integer database;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.lettuce.pool.max-active&#125;&quot;)</span><br><span class="line">    private Integer maxActive;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.lettuce.pool.max-idle&#125;&quot;)</span><br><span class="line">    private Integer maxIdle;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.lettuce.pool.max-wait&#125;&quot;)</span><br><span class="line">    private Long maxWait;</span><br><span class="line">    @Value(&quot;$&#123;spring.redis.lettuce.pool.min-idle&#125;&quot;)</span><br><span class="line">    private Integer minIdle;</span><br><span class="line"></span><br><span class="line">    @Bean</span><br><span class="line">    public LettuceConnectionFactory lettuceConnectionFactory() &#123;</span><br><span class="line">        GenericObjectPoolConfig&lt;Object&gt; genericObjectPoolConfig &#x3D; new GenericObjectPoolConfig&lt;&gt;();</span><br><span class="line">        genericObjectPoolConfig.setMaxIdle(maxIdle);</span><br><span class="line">        genericObjectPoolConfig.setMinIdle(minIdle);</span><br><span class="line">        genericObjectPoolConfig.setMaxTotal(maxActive);</span><br><span class="line">        genericObjectPoolConfig.setMaxWaitMillis(maxWait);</span><br><span class="line">        genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(100);</span><br><span class="line">        RedisStandaloneConfiguration redisStandaloneConfiguration &#x3D; new RedisStandaloneConfiguration();</span><br><span class="line">        redisStandaloneConfiguration.setDatabase(database);</span><br><span class="line">        redisStandaloneConfiguration.setHostName(host);</span><br><span class="line">        redisStandaloneConfiguration.setPort(port);</span><br><span class="line">        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));</span><br><span class="line">        LettuceClientConfiguration clientConfig &#x3D; LettucePoolingClientConfiguration.builder()</span><br><span class="line">                .poolConfig(genericObjectPoolConfig)</span><br><span class="line">                .build();</span><br><span class="line">        return new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="6、总结"><a href="#6、总结" class="headerlink" title="6、总结"></a>6、总结</h2><p><strong>① 全面理解新特性</strong>。实际上我们会发现，正是 JDK8 之后接口中多了默认方法的实现，一些老版本的工具包的类才得以兼容，但是今天遇到的这个异常还真就是因为默认方法的实现，导致了找不到实现类一直调接口自己的默认方法，只能说有利有弊吧；</p>
<p><strong>② 尽量选择稳定版本</strong>。对于工具/框架的使用尽量使用修复已知 BUG 的稳定版本，否则因为一个小缺陷可能就要花上大半天去解决；</p>
<p><strong>③ 持续锻炼排错能力</strong>。对于工具/框架内部某些方法的报错信息，还是要坚持打断点去调试，经过一轮探索之后的收获远强于自己去网上Copy相应的解决方案，在这个过程中对排错能力也有较大提升。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">h0ss</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://blog.gpnusz.cn/2022/08/21/%E3%80%90Debug%E3%80%91bitField-%E5%BC%95%E5%8F%91%E7%9A%84%E6%A0%88%E6%BA%A2%E5%87%BA%E6%8E%92%E9%94%99%E8%AE%B0/">https://blog.gpnusz.cn/2022/08/21/%E3%80%90Debug%E3%80%91bitField-%E5%BC%95%E5%8F%91%E7%9A%84%E6%A0%88%E6%BA%A2%E5%87%BA%E6%8E%92%E9%94%99%E8%AE%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.gpnusz.cn" target="_blank">后端学习记录</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/09/03/%E3%80%90MySQL%E3%80%91InnoDB%E7%9A%84%E8%A1%8C%E6%A0%BC%E5%BC%8F/"><img class="prev-cover" src="https://i.loli.net/2021/09/01/PvqRnTrUBh1a37J.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【MySQL】InnoDB的行格式</div></div></a></div><div class="next-post pull-right"><a href="/2022/07/16/%E3%80%90RPC%E3%80%91%E8%AF%A6%E8%A7%A3SPI%E6%9C%BA%E5%88%B6/"><img class="next-cover" src="https://i.loli.net/2021/09/01/PvqRnTrUBh1a37J.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【RPC】详解SPI机制</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/11/16/给开源项目提了个PR/" title="给开源项目提了个PR"><img class="cover" src="https://i.loli.net/2021/09/01/PRTlcsGZMQ7yBqn.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-16</div><div class="title">给开源项目提了个PR</div></div></a></div><div><a href="/2022/07/16/【RPC】详解SPI机制/" title="【RPC】详解SPI机制"><img class="cover" src="https://i.loli.net/2021/09/01/PvqRnTrUBh1a37J.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-16</div><div class="title">【RPC】详解SPI机制</div></div></a></div><div><a href="/2022/07/10/【RPC】基本网络通信模型/" title="【RPC】基本网络通信模型"><img class="cover" src="https://i.loli.net/2021/09/01/PvqRnTrUBh1a37J.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-10</div><div class="title">【RPC】基本网络通信模型</div></div></a></div><div><a href="/2022/07/02/【JVM】类加载全过程简记/" title="【JVM】类加载全过程简记"><img class="cover" src="https://i.loli.net/2021/09/01/PRTlcsGZMQ7yBqn.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-07-02</div><div class="title">【JVM】类加载全过程简记</div></div></a></div><div><a href="/2022/06/28/【RPC】RPC实战与核心的学习笔记/" title="【RPC】RPC实战与核心的学习笔记"><img class="cover" src="https://i.loli.net/2021/09/01/PvqRnTrUBh1a37J.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-28</div><div class="title">【RPC】RPC实战与核心的学习笔记</div></div></a></div><div><a href="/2022/05/09/【Debug】Dubbo异常处理排错记/" title="【Debug】Dubbo异常处理排错记"><img class="cover" src="https://i.loli.net/2021/09/01/PRTlcsGZMQ7yBqn.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-09</div><div class="title">【Debug】Dubbo异常处理排错记</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">h0ss</div><div class="author-info__description">一个苦逼后端仔的学习记录</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">51</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/h0ss"><i class="fab fa-github"></i><span>Gitee仓库</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">Java后端技术栈语雀文档：https://www.yuque.com/oliver-wdtb1/java</div></div><div class="sticky_layout"><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/11/16/%E7%BB%99%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E6%8F%90%E4%BA%86%E4%B8%AAPR/" title="给开源项目提了个PR">给开源项目提了个PR</a><time datetime="2022-11-15T17:28:40.000Z" title="发表于 2022-11-16 01:28:40">2022-11-16</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/09/03/%E3%80%90MySQL%E3%80%91InnoDB%E7%9A%84%E8%A1%8C%E6%A0%BC%E5%BC%8F/" title="【MySQL】InnoDB的行格式">【MySQL】InnoDB的行格式</a><time datetime="2022-09-02T18:17:38.000Z" title="发表于 2022-09-03 02:17:38">2022-09-03</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/08/21/%E3%80%90Debug%E3%80%91bitField-%E5%BC%95%E5%8F%91%E7%9A%84%E6%A0%88%E6%BA%A2%E5%87%BA%E6%8E%92%E9%94%99%E8%AE%B0/" title="【Debug】bitField 引发的栈溢出排错记">【Debug】bitField 引发的栈溢出排错记</a><time datetime="2022-08-20T18:10:40.000Z" title="发表于 2022-08-21 02:10:40">2022-08-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/07/16/%E3%80%90RPC%E3%80%91%E8%AF%A6%E8%A7%A3SPI%E6%9C%BA%E5%88%B6/" title="【RPC】详解SPI机制">【RPC】详解SPI机制</a><time datetime="2022-07-15T18:51:57.000Z" title="发表于 2022-07-16 02:51:57">2022-07-16</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2022/07/14/%E3%80%90Redis%E3%80%91%E9%87%8D%E8%A6%81%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%9F%BA%E7%A1%80/" title="【Redis】重要数据结构基础">【Redis】重要数据结构基础</a><time datetime="2022-07-13T17:22:08.000Z" title="发表于 2022-07-14 01:22:08">2022-07-14</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="爱国,敬业,诚信,友善,自由,平等,公正,法治,富强,民主,文明,和谐" data-fontsize="16px" data-random="false" async="async"></script></div></body></html>